package com.cskaoyan.service;

import com.cskaoyan.bean.PO.*;
import com.cskaoyan.bean.VO.*;
import com.cskaoyan.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wenkaikai
 * @since 2022/08/17 10:03
 */
@Service
@Transactional
public class WxHomeServiceImpl implements WxHomeService {


    @Autowired
    MarketAdMapper marketAdMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public WxHomeIndexVO queryHomeIndex() {
        WxHomeIndexVO wxHomeIndexVO = new WxHomeIndexVO();
        // 首先得到广告横幅，enabled 为 true 使用广告条件
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria marketAdExampleCriteria = marketAdExample.createCriteria();
        marketAdExampleCriteria.andEnabledEqualTo(true);
        marketAdExampleCriteria.andDeletedEqualTo(false);
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        // 广告内容放入
        wxHomeIndexVO.setBanner(marketAds);

        // 品牌列表
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria marketBrandExampleCriteria = marketBrandExample.createCriteria();
        marketBrandExampleCriteria.andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        ArrayList<MarketBrand> brandArrayList = new ArrayList<>();
        int brandNum = 1;
        for (MarketBrand marketBrand : marketBrands) {
            if(brandNum > 5){
                break;
            }
            brandArrayList.add(marketBrand);
            brandNum++;
        }
        // 品牌列表放入
        wxHomeIndexVO.setBrandList(brandArrayList);

        // 图标和第一层categories，pid为0的第一层列表
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andPidEqualTo(0);
        // 没有被删除
        categoryExampleCriteria.andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(categoryExample);
        // 里面参数太多，不全部返回，只返回3个参数
        ArrayList<WxHomeIndexInnerChannelVO> wxHomeIndexInnerChannelVOS = new ArrayList<>();
        for (MarketCategory category : marketCategories) {
            wxHomeIndexInnerChannelVOS.add(new WxHomeIndexInnerChannelVO(category.getId(), category.getName(), category.getIconUrl()));
        }
        // 放入channel列表
        wxHomeIndexVO.setChannel(wxHomeIndexInnerChannelVOS);

        // 优惠券列表,逻辑有写复杂 ，等到写优惠券，一起修改
        // TODO
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria couponExampleCriteria = marketCouponExample.createCriteria();
        // 优惠券的状态为0表示正常，其他不正常的不应该显示在页面
        couponExampleCriteria.andStatusEqualTo((short) 0);
        couponExampleCriteria.andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        // 放入
        wxHomeIndexVO.setCouponList(marketCoupons);

       // ----------------------------
        // 放第一层商品列表，但是只显示前5个类目的商品,并且每个类目只显示里面6个商品信息
        ArrayList<WxHomeIndexInnerFloorGLVO> wxHomeIndexInnerFloorGLVOS = new ArrayList<>();
        // 根据类目id查找里面的商品
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsExampleCriteria = marketGoodsExample.createCriteria();
        int categoryNum = 1;
        for (WxHomeIndexInnerChannelVO innerChannelVO : wxHomeIndexInnerChannelVOS) {
            if (categoryNum > 5) {
                break;
            }
            // 第一级类目 id
            Integer categoryId = innerChannelVO.getId();
            // 找到第一级类目下的第一个第二级类目id
            MarketCategory marketCategory = marketCategoryMapper.selectFirstSecCategory(categoryId);
            // 根据二级类目找商品
            goodsExampleCriteria.andCategoryIdEqualTo(marketCategory.getId());
            List<MarketGoodsVo> marketGoodsVos = marketGoodsMapper.selectByExampleVo(marketGoodsExample);
            // 得到一个类目下的商品列表,只选前6个
            if (marketGoodsVos == null || marketGoodsVos.size() == 0) {
                // 如果该类目无商品，则跳过，选下一个
                marketGoodsExample.clear();
                continue;
            }
            // 建一个外层类
            WxHomeIndexInnerFloorGLVO wxHomeIndexInnerFloorGLVO = new WxHomeIndexInnerFloorGLVO();
            wxHomeIndexInnerFloorGLVO.setId(marketCategory.getId());
            wxHomeIndexInnerFloorGLVO.setName(innerChannelVO.getName());
            // 新建一个goodList
            ArrayList<WxHomeIndexGoodsVO> goodList = new ArrayList<>();
            int goodsNum = 1;
            for (MarketGoodsVo marketGoodsVo : marketGoodsVos) {
                if (goodsNum > 6) {
                    break;
                }
                goodList.add(new WxHomeIndexGoodsVO(marketGoodsVo.getId(), marketGoodsVo.getIsHot(), marketGoodsVo.getIsNew(),
                        marketGoodsVo.getName(), marketGoodsVo.getPicUrl(), marketGoodsVo.getBrief(), marketGoodsVo.getCounterPrice(), marketGoodsVo.getRetailPrice()));
                goodsNum++;
            }
            wxHomeIndexInnerFloorGLVO.setGoodsList(goodList);
            marketGoodsExample.clear();
            categoryNum++;
            wxHomeIndexInnerFloorGLVOS.add(wxHomeIndexInnerFloorGLVO);
        }
        // 放入
        wxHomeIndexVO.setFloorGoodsList(wxHomeIndexInnerFloorGLVOS);

        // 找热点商品
        goodsExampleCriteria.andIsHotEqualTo(true);
        List<MarketGoodsVo> marketHotGoodsVos = marketGoodsMapper.selectByExampleVo(marketGoodsExample);
        marketGoodsExample.clear();
        ArrayList<WxHomeIndexGoodsVO> hotGoodsList = new ArrayList<>();
        int hotGoodsNum = 1;
        for (MarketGoodsVo marketHotGoodsVo : marketHotGoodsVos) {
            if(hotGoodsNum > 5){
                // 放5个在页面
                break;
            }
            hotGoodsList.add(new WxHomeIndexGoodsVO(marketHotGoodsVo.getId(),marketHotGoodsVo.getIsHot(),marketHotGoodsVo.getIsNew(),
                    marketHotGoodsVo.getName(),marketHotGoodsVo.getPicUrl(),marketHotGoodsVo.getBrief(),marketHotGoodsVo.getCounterPrice(),
                    marketHotGoodsVo.getRetailPrice()));
            hotGoodsNum++;
        }
        // 放入热点商品
        wxHomeIndexVO.setHotGoodsList(hotGoodsList);

        // 找新品
        goodsExampleCriteria.andIsNewEqualTo(true);
        List<MarketGoodsVo> marketNewGoodsVos = marketGoodsMapper.selectByExampleVo(marketGoodsExample);
        marketGoodsExample.clear();
        ArrayList<WxHomeIndexGoodsVO> newGoodsList = new ArrayList<>();
        int newGoodsNum = 1;
        for (MarketGoodsVo newGoodsVo : marketNewGoodsVos) {
            if(newGoodsNum > 5){
                // 最多放5个
                break;
            }
            newGoodsList.add(new WxHomeIndexGoodsVO(newGoodsVo.getId(),newGoodsVo.getIsHot(),newGoodsVo.getIsNew(),
                    newGoodsVo.getName(),newGoodsVo.getPicUrl(),newGoodsVo.getBrief(),newGoodsVo.getCounterPrice(),
                    newGoodsVo.getRetailPrice()));
            newGoodsNum++;
        }
        // 放入新品
        wxHomeIndexVO.setNewGoodsList(newGoodsList);

        // 找专题
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(topicExample);
        ArrayList<MarketTopic> marketTopicsVo = new ArrayList<>();
        int topicNum = 1;
        for (MarketTopic marketTopic : marketTopics) {
            if(topicNum > 4){
                break;
                // 最多放4个专题
            }
            marketTopicsVo.add(marketTopic);
            topicNum++;
        }
        // 放入
        wxHomeIndexVO.setTopicList(marketTopicsVo);
        return wxHomeIndexVO;
    }
}